package com.android.job.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.job.fusion.FusionCode;
import com.android.job.logic.SearchJobLogic;
import com.android.job.logic.SearchResumeLogic;
import com.android.job.model.Job;
import com.android.job.model.JobItem;
import com.android.job.ui.adapter.JobItemAdapter;

/**
 * 搜索职位界面
 * 
 * 
 */
public class JobActivity extends BaseActivity implements OnClickListener,
		android.content.DialogInterface.OnClickListener
{
	private TextView salaryTV;
	private TextView regionTV;
	private TextView jobTypeTV;
	private TextView updateTV;
	private TextView degreeTV;
	private TextView sizeTV;
	private TextView loadMore;
	private Button search_bt_id;
	private Button back;
	private Button salaryBtn;
	private Button regionBtn;
	private Button degreeBtn;
	private Button updateBtn;
	private Button jobtypeBtn;

	private String jobType="";
	private String update="";
	private String salary="";
	private String region="";
	private String degress="";

	private RelativeLayout search_layout;
	private RelativeLayout result_layout;
	private ListView resultLst;
	private int more = 0;

	private static int whichDialog;

	private RelativeLayout salary_layout;
	private RelativeLayout region_layout;
	private RelativeLayout type_layout;
	private RelativeLayout degree_layout;
	private RelativeLayout updatetime_layout;
	private View loadMoreView; 
	private ProgressDialog pd;
	private ArrayList<Job> jobs;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.job_search);
		jobs = new ArrayList<Job>();
		initLayout();
	}

	private void initLayout()
	{
		pd = new ProgressDialog(JobActivity.this);
		pd.setCancelable(true);
		pd.setMessage(getString(R.string.search_loading));

		jobTypeTV = (TextView) findViewById(R.id.job_type_et_id);
		updateTV = (TextView) findViewById(R.id.update_time_et_id);
		salaryTV = (TextView) findViewById(R.id.salary_range_et_id);
		regionTV = (TextView) findViewById(R.id.region_et);
		degreeTV = (TextView) findViewById(R.id.degree_et_id);
		sizeTV = (TextView) findViewById(R.id.result_size);

		search_bt_id = (Button) findViewById(R.id.search_bt_id);
		search_bt_id.setOnClickListener(this);

		back = (Button) findViewById(R.id.search_back);
		back.setOnClickListener(this);

		salaryBtn = (Button) findViewById(R.id.popup_chose_salary);
		salaryBtn.setOnClickListener(this);

		regionBtn = (Button) findViewById(R.id.popup_chose_region);
		regionBtn.setOnClickListener(this);

		jobtypeBtn = (Button) findViewById(R.id.popup_chose_job_type);
		jobtypeBtn.setOnClickListener(this);

		degreeBtn = (Button) findViewById(R.id.popup_chose_degree);
		degreeBtn.setOnClickListener(this);

		updateBtn = (Button) findViewById(R.id.popup_chose_update_time);
		updateBtn.setOnClickListener(this);

		search_layout = (RelativeLayout) findViewById(R.id.search_layout);
		result_layout = (RelativeLayout) findViewById(R.id.result_layout);

		resultLst = (ListView) findViewById(R.id.search_result_list);

		salary_layout = (RelativeLayout) findViewById(R.id.salary);
		region_layout = (RelativeLayout) findViewById(R.id.region);
		type_layout = (RelativeLayout) findViewById(R.id.job_type);
		degree_layout = (RelativeLayout) findViewById(R.id.degree);
		updatetime_layout = (RelativeLayout) findViewById(R.id.update_time);

		
		loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null); 
		loadMore = (TextView)loadMoreView.findViewById(R.id.add_more); 
		loadMore.setOnClickListener(JobActivity.this);
		resultLst.addFooterView(loadMoreView);
		
		salary_layout.setOnClickListener(this);
		region_layout.setOnClickListener(this);
		type_layout.setOnClickListener(this);
		degree_layout.setOnClickListener(this);
		updatetime_layout.setOnClickListener(this);
	}

	private void getKeywords()
	{
		jobType = jobTypeTV.getText().toString();
		update = updateTV.getText().toString();
		salary = salaryTV.getText().toString();
		region = regionTV.getText().toString();
		degress = degreeTV.getText().toString();
	}

	private Handler handler = new Handler()
	{

		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case FusionCode.NETWORK_ERROR:
					pd.dismiss();
					Toast.makeText(JobActivity.this, R.string.network_error,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.NETWORK_TIMEOUT_ERROR:
					pd.dismiss();
					Toast.makeText(JobActivity.this, R.string.network_timeout,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.RETURN_JSONOBJECT:
					pd.dismiss();
					search_layout.setVisibility(View.GONE);
					result_layout.setVisibility(View.VISIBLE);
					ArrayList<Job> temp = (ArrayList<Job>) msg.obj;
					jobs.addAll(temp);
					sizeTV.setText(getString(R.string.result_start)+jobs.size()+getString(R.string.result_end));
					JobItemAdapter adapter = new JobItemAdapter(JobActivity.this, jobs);
					resultLst.setAdapter(adapter);
					resultLst.setOnItemClickListener(new OnItemClickListener()
					{
						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3)
						{
							int size = jobs.size();
							
							if(position<size)
							{
								Intent intent = new Intent(JobActivity.this,
										JobDetailActivity.class);
								Bundle bundle = new Bundle();
								bundle.putSerializable("jobdetail", jobs.get(position));
								intent.putExtras(bundle);
								JobActivity.this.startActivity(intent);
							}
							else
							{
								pd.show();
								more = more + 20;
								SearchJobLogic.getInstance().searchJob(handler, salary, region,
										jobType, degress, update, String.valueOf(more));
							}
						}
					});
					break;
				case FusionCode.SERVER_ERROR:
					pd.dismiss();
					String errorInif = (String) msg.obj;
					
					Toast.makeText(JobActivity.this,
							errorInif, Toast.LENGTH_SHORT)
							.show();
					break;
			}

		};
	};

	@Override
	protected void onResume()
	{
		super.onResume();
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		AlertDialog.Builder builder = null;
		switch (id)
		{
			case FusionCode.RESUME_SEARCH_SALARY_RANGE:
				builder = new AlertDialog.Builder(this).setItems(
						R.array.salary_range, this);
				dialog = builder.create();
				whichDialog = FusionCode.RESUME_SEARCH_SALARY_RANGE;
				break;
			case FusionCode.RESUME_SEARCH_REGION:
				builder = new AlertDialog.Builder(this).setItems(
						R.array.region, this);
				dialog = builder.create();
				whichDialog = FusionCode.RESUME_SEARCH_REGION;
				break;
			case FusionCode.RESUME_SEARCH_JOB_TYPE:
				builder = new AlertDialog.Builder(this).setItems(
						R.array.job_type, this);
				dialog = builder.create();
				whichDialog = FusionCode.RESUME_SEARCH_JOB_TYPE;
				break;
			case FusionCode.RESUME_SEARCH_DEGREE:
				builder = new AlertDialog.Builder(this).setItems(
						R.array.degree, this);
				dialog = builder.create();
				whichDialog = FusionCode.RESUME_SEARCH_DEGREE;
				break;
			default:
				return super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.search_bt_id:
//				 getKeywords();
				pd.show();
				SearchJobLogic.getInstance().searchJob(handler, salary, region,
						jobType, degress, update, "0");
//				 SearchJobLogic.getInstance().searchJob(handler, "", "320700",
//				 "", "", "", "");

				// handler.sendEmptyMessageAtTime(3, 2000);
				break;
			case R.id.search_back:
				finish();
				break;
			case R.id.popup_chose_salary:
				// showDialog(FusionCode.RESUME_SEARCH_SALARY_RANGE);
				// jumpToSelect( FusionCode.SALARY_ITEM );
				break;
			case R.id.region_choose:
				// showDialog(FusionCode.RESUME_SEARCH_REGION);
				break;
			case R.id.degree_choose:
				showDialog(FusionCode.RESUME_SEARCH_DEGREE);
				break;
			case R.id.popup_chose_update_time:
				break;
			case R.id.popup_chose_job_type:
				break;
			case R.id.salary:
				jumpToSelect(FusionCode.SALARY_ITEM, R.id.salary_range_et_id);
				break;
			case R.id.region:
				jumpToSelect(FusionCode.CITY_ITEM_ONE, R.id.region_et_id);
				break;
			case R.id.job_type:
				jumpToSelect(FusionCode.JOB_TYPE_ITEM, R.id.job_type_et_id);
				break;
			case R.id.degree:
				jumpToSelect(FusionCode.DEGREE_ITEM, R.id.degree_et_id);
				break;
			case R.id.update_time:
				jumpToSelect(FusionCode.TIME_ITEM, R.id.update_time_et_id);
				break;
			case R.id.add_more:
				pd.show();
				more = more + 20;
				SearchJobLogic.getInstance().searchJob(handler, salary, region,
						jobType, degress, update, String.valueOf(more));
				break;
			default:
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		if (requestCode == 0 && resultCode == 0)
		{
			if (data != null)
			{
				int id = data.getIntExtra("view_id", 0);
				String choose = data.getStringExtra("chose");
				String text = data.getStringExtra("text");
				switch (id)
				{
					case R.id.salary_range_et_id:
						salary = choose;
						salaryTV.setText(text);
						break;
					case R.id.region_et_id:
						region = choose;
						regionTV.setText(text);
						break;
					case R.id.job_type_et_id:
						jobType = choose;
						jobTypeTV.setText(text);
						break;
					case R.id.degree_et_id:
						degress = choose;
						degreeTV.setText(text);
						break;
					case R.id.update_time_et_id:
						update = choose;
						updateTV.setText(text);
						break;
					default:
						break;
				}
			}

		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		if (event.getAction() == KeyEvent.ACTION_DOWN)
		{
			switch (event.getKeyCode())
			{
				case KeyEvent.KEYCODE_BACK:
				{
					if (result_layout.getVisibility() == View.VISIBLE)
					{
						result_layout.setVisibility(View.GONE);
						search_layout.setVisibility(View.VISIBLE);
						jobs.clear();
//						clearKey();
						return true;
					}

					if (search_layout.getVisibility() == View.VISIBLE)
					{
						finish();
						return true;
					}
				}

			}
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	public void onClick(DialogInterface dialog, int which)
	{
		switch (whichDialog)
		{
			case FusionCode.RESUME_SEARCH_SALARY_RANGE:
				salaryTV.setText(this.getResources().getStringArray(
						R.array.salary_range)[which]);
				break;
			case FusionCode.RESUME_SEARCH_REGION:
				regionTV.setText(this.getResources().getStringArray(
						R.array.region)[which]);
				break;
			case FusionCode.RESUME_SEARCH_DEGREE:
				degreeTV.setText(this.getResources().getStringArray(
						R.array.degree)[which]);
				break;
		}
	}
	
	
	private void clearKey(){
		jobType="";
		update="";
		salary="";
		region="";
		degress="";
		
		salaryTV.setText("");
		regionTV.setText("");
		jobTypeTV.setText("");
		updateTV.setText("");
		degreeTV.setText("");
	}

}
